package kau.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kau.dao.CurriPlanDAOImpl;
import kau.dao.CurriculumDAOImpl;
import kau.dao.CustomerDAOImpl;
import kau.vo.CurriPlan;
import kau.vo.Curriculum;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
 
public class CurriculumAction extends MultiActionController {
	
	public ModelAndView getCurriculum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String custId = request.getParameter("custId");
		int custLevel = CustomerDAOImpl.getInstance().getCustomer(custId).getCustLevel();
		ModelAndView nextPage = new ModelAndView();
		Curriculum curriculum = CurriculumDAOImpl.getInstance().getCurriculum(Integer.parseInt(request.getParameter("courseId")));
		ArrayList<CurriPlan> curriPlanList
			=(ArrayList)CurriPlanDAOImpl.getInstance().getAllCurriPlans(Integer.parseInt(request.getParameter("courseId")));
		request.setAttribute("curriculum", curriculum);
		request.setAttribute("curriPlanList", curriPlanList);
		System.out.println(curriPlanList);
		System.out.println(custLevel);
		if(custLevel == 3){
			nextPage.setViewName("/course/index.jsp?content=/course/curriculum/curriculumPage.jsp");
		}
		else{
			nextPage.setViewName("/course/index.jsp?content=/course/curriculum/update_curriculum.jsp");
		}
		return nextPage;
	}

	public ModelAndView update_curriculum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView nextPage = new ModelAndView();
		String custId = request.getParameter("custId");
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		String subjectName = request.getParameter("subjectName");
		String lecturerName = request.getParameter("lecturerName");
		int weeks = Integer.parseInt(request.getParameter("weeks"));
		int hourperWeek = Integer.parseInt(request.getParameter("hourperWeek"));
		String evaluation = request.getParameter("evaluation");
		String textBook = request.getParameter("textBook");
		String author = request.getParameter("author");
		String referenceBook = request.getParameter("referenceBook");
		String referenceAuthor = request.getParameter("referenceAuthor");
		String overview = request.getParameter("overview");
		
		Curriculum curriculum = new Curriculum(courseId, subjectName, lecturerName, weeks, hourperWeek, overview, evaluation, textBook, author, referenceBook, referenceAuthor);
		
		CurriculumDAOImpl.getInstance().update(curriculum);
		
		
		
		
		
		for(int week=1;week<=weeks;week++){
			String content =request.getParameter("inputContent["+week+"]");
			
			CurriPlan curriPlan = new CurriPlan(courseId, week, content);
			CurriPlanDAOImpl.getInstance().update(curriPlan);

		}
		
		nextPage.setViewName("getCurriculum.curriculum?custId="+custId+"&courseId"+courseId);
		return nextPage;
	}
}
